package boot

import (
	"gitee.com/jjawesomejj/awesome-util/config"
	"gitee.com/jjawesomejj/awesome-util/helper/commonhelper"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

var dbConn *gorm.DB

type DatabaseService struct {
}

func GetDbConnection() *gorm.DB {
	db := dbConn
	return db
}
func (service *DatabaseService) Init() {
	dsn := commonhelper.Format("{}:{}@tcp({}:{})/{}?charset=utf8mb4&parseTime=True&loc=Local",
		config.GetConfigByKey("database.user").(string),
		config.GetConfigByKey("database.password").(string),
		config.GetConfigByKey("database.host").(string),
		config.GetConfigByKey("database.port").(string),
		config.GetConfigByKey("database.database").(string),
	)
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}
	dbConn = db
}
